﻿package core.dialog {
	import core.dialog .DialogMessage;
	import base.BaseMc;
	import core.display.RankItem;
	import core.display.RankItemModule;
	import utils.UtilScreen;
	import core.events.CmdEvent;
	import core.net.socket.cmd.GetFlowerRankCmd;
	import com.adobe.serialization.json.JSON;
	
	public class DialogRank extends BaseMc {

		private  var viewArray:Array = new Array(5);
		private  var pageIndex:int =0;
		private  var pageSize:int =5;
		private  var dataArr:Array;
		
		public function DialogRank() {
			// constructor code
		}
		
		//当影片放到场景上后的初始化方法
		protected override function onInit():void {
			//this.centerOfStage();
			
			this.initCompents();
			this.sendCmd(GetFlowerRankCmd.make());
			this.scale(1.5,1.5);
			this.x = UtilScreen.getScreenWidth()/2 - this.width/2;
			this.y = UtilScreen.getScreenHeight()/2 - this.height/2;
			this.centerOfStage();
		}
		
		//当影片从场景上移除的时候周期方法 
		protected override function onRemove():void {
			
		}
		
		private function initCompents():void {

			for(var i:int=0; i<pageSize;i++ ){
				var item: RankItem  = new RankItem();
				item.x=40;
				item.y = 80+(75*i) ;
				item.visible = false;
				addChild(item);
				viewArray[i] = item;
			}
			
			
		}
		
		
		 private function isCurrentPage(element:*, index:int, arr:Array):Boolean {
			var elementIndex :  int =  arr.indexOf(element);
			return elementIndex >= pageSize * pageIndex && elementIndex < pageSize*(pageIndex + 1);
        }
		
		private function totalPageCounts(): int{
			var totalpage : int = 0;
			if(dataArr.length % pageSize == 0){
  				totalpage = dataArr.length / pageSize;
			}else{
         	  totalpage= dataArr.length / pageSize + 1;
			}
			
			return totalpage;
		} 

		
		public function setRankDialogData(): void{
			
		 	var currentArray:Array = dataArr.filter(isCurrentPage);

			if ( viewArray.length == pageSize && currentArray.length <= pageSize) {
			
				for(var i:int=0; i < viewArray.length;i++ ){
					viewArray[i].visible = false;
					if(i <= currentArray.length-1){
						viewArray[i].setItemData(currentArray[i]);
						viewArray[i].visible = true;
					}else{
						viewArray[i].visible = false;
					}
					
				}	
			
			}
			
		}
		
		
		public function setPrePageData():void{
			if(pageIndex == 0){
				var dialogMessage: DialogMessage = new DialogMessage("没有更多数据",true);
				this.parent.addChild(dialogMessage);
				 return;
			}
			pageIndex --;
			setRankDialogData();
						
		}
		
		public function setNextPageData():void{
			var totalCounts: int = totalPageCounts();
			if(pageIndex >= totalCounts - 1){
				var dialogMessage: DialogMessage = new DialogMessage("没有更多数据",true);
				this.parent.addChild(dialogMessage);
				return;
			}
			pageIndex++;
			setRankDialogData();
		}
		
		
		protected override function onCommandResponse(e:CmdEvent):void {
			super.onCommandResponse(e);
			
			var data:Object = e.data;
			if(data.cmd == GetFlowerRankCmd.COMMAND_ID){
				if(data.error != null){
					trace(data.error.toString());
					dataArr = com.adobe.serialization.json.JSON.decode( data.error.toString());
					if(dataArr != null){
						for(var i:int=0;i<dataArr.length;i++){
							dataArr[i].rank = (i+1).toString();
						
						}
						setRankDialogData();
					}
				}else{
					//失败
					trace("排行榜请求数据失败");
				}
			}
		}
	
	
	}
	
}
